﻿package mortal.game.view.guide
{
    import com.gengine.resource.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import flash.events.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.game.view.common.*;

    public class GuideArrow extends GSprite
    {
        private var _container:GSprite;
        protected var _bmp:GImageBitmap;
        private var _txt:GTextFiled;
        private var _curDirection:int = -1;
        private var _moveDistance:int = 20;
        private var _n:int = 0;
        private var _stepAdd:Number = 0.6;
        private var isLoaded:Boolean = false;
        private var isLoading:Boolean = false;
        private var _lastSetted:int = -1111;
        public static const Left:int = 1;
        public static const Right:int = 2;
        public static const Up:int = 3;
        public static const Down:int = 4;
        private static const vh:int = 130;
        private static const vw:int = 160;
        private static const hh:int = 70;
        private static const hw:int = 210;

        public function GuideArrow()
        {
            return;
        }// end function

        public function get moveDistance() : int
        {
            return this._moveDistance;
        }// end function

        public function set moveDistance(param1:int) : void
        {
            this._moveDistance = param1;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._container = UICompomentPool.getUICompoment(GSprite);
            this.addChild(this._container);
            this._bmp = UIFactory.gImageBitmap("", 0, 0, this._container);
            var _loc_1:* = GlobalStyle.textFormatPutong;
            _loc_1.align = TextFormatAlign.CENTER;
            _loc_1.leading = 0;
            _loc_1.size = 14;
            _loc_1.color = 16777028;
            this._txt = UIFactory.gTextField("", 0, 2, 130, 60, this._container, _loc_1);
            this._txt.multiline = true;
            this._txt.wordWrap = true;
            this._txt.text = "";
            this.mouseChildren = false;
            this.mouseEnabled = false;
            this._n = 0;
            this.addEventListener(Event.ENTER_FRAME, this.everyFrameHandler);
            if (!this.isLoaded && !this.isLoading)
            {
                this.isLoading = true;
                this.loadArrowRes();
            }
            return;
        }// end function

        protected function loadArrowRes() : void
        {
            LoaderHelp.addResCallBack("guideArrow_H", this.loadHComplete);
            return;
        }// end function

        private function loadHComplete() : void
        {
            LoaderHelp.addResCallBack("guideArrow_V", this.loadResComplete);
            return;
        }// end function

        protected function loadResComplete() : void
        {
            this.isLoading = false;
            this.isLoaded = true;
            this.updateDirection();
            this.updateTextPlace();
            return;
        }// end function

        private function everyFrameHandler(event:Event) : void
        {
            if (super.isDisposed)
            {
                return;
            }
            if (!this.isLoaded)
            {
                return;
            }
            var _loc_3:String = this;
            var _loc_4:* = this._n + 1;
            _loc_3._n = _loc_4;
            var _loc_2:* = this._n * this._stepAdd;
            if (_loc_2 / this._moveDistance >= 2)
            {
                this._n = 0;
                this._stepAdd = 0.6;
            }
            if (_loc_2 < this._moveDistance + 5)
            {
                this._stepAdd = this._stepAdd + 0.01;
            }
            _loc_2 = Math.abs(_loc_2 - this._moveDistance);
            switch(this._curDirection)
            {
                case Right:
                {
                    this._container.x = -_loc_2;
                    this._container.y = 0;
                    break;
                }
                case Left:
                {
                    this._container.x = _loc_2;
                    this._container.y = 0;
                    break;
                }
                case Up:
                {
                    this._container.y = _loc_2;
                    this._container.x = 0;
                    break;
                }
                case Down:
                {
                    this._container.y = -_loc_2;
                    this._container.x = 0;
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public function set direction(param1:int) : void
        {
            this._curDirection = param1;
            if (!this.isLoaded)
            {
                return;
            }
            if (_disposed)
            {
                return;
            }
            this.updateDirection();
            this.updateTextPlace();
            return;
        }// end function

        public function get direction() : int
        {
            return this._curDirection;
        }// end function

        private function updateDirection() : void
        {
            if (super.isDisposed)
            {
                return;
            }
            if (this._lastSetted == this._curDirection)
            {
                return;
            }
            this._lastSetted = this._curDirection;
            if (!this.isLoaded)
            {
                return;
            }
            this.setBitmapdataByDir();
            switch(this._curDirection)
            {
                case Right:
                {
                    this._bmp.rotation = 0;
                    this._bmp.x = -hw;
                    this._bmp.y = (-hh) / 2;
                    break;
                }
                case Left:
                {
                    this._bmp.rotation = 180;
                    this._bmp.x = hw;
                    this._bmp.y = hh / 2;
                    break;
                }
                case Up:
                {
                    this._bmp.rotation = 180;
                    this._bmp.y = vh;
                    this._bmp.x = vw / 2;
                    break;
                }
                case Down:
                {
                    this._bmp.rotation = 0;
                    this._bmp.y = -vh;
                    this._bmp.x = (-vw) / 2;
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        protected function setBitmapdataByDir() : void
        {
            switch(this._curDirection)
            {
                case Right:
                {
                    LoaderHelp.setBitmapdata("guideArrow_H.swf", this._bmp);
                    break;
                }
                case Left:
                {
                    LoaderHelp.setBitmapdata("guideArrow_H.swf", this._bmp);
                    break;
                }
                case Up:
                {
                    LoaderHelp.setBitmapdata("guideArrow_V.swf", this._bmp);
                    break;
                }
                case Down:
                {
                    LoaderHelp.setBitmapdata("guideArrow_V.swf", this._bmp);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public function set label(param1:String) : void
        {
            if (super.isDisposed)
            {
                return;
            }
            this._txt.htmlText = HTMLUtil.getHtmlByComp(param1);
            this.updateTextPlace();
            this.direction = this._curDirection;
            return;
        }// end function

        private function updateTextPlace() : void
        {
            if (super.isDisposed)
            {
                return;
            }
            switch(this._curDirection)
            {
                case Right:
                {
                    this._txt.x = this._bmp.x + 13;
                    this._txt.y = (-this.getLabelHeight()) / 2;
                    break;
                }
                case Left:
                {
                    this._txt.x = hw - 130 - 13;
                    this._txt.y = (-this.getLabelHeight()) / 2;
                    break;
                }
                case Up:
                {
                    this._txt.x = -130 / 2;
                    this._txt.y = vh - 75 + (75 - this.getLabelHeight()) / 2;
                    break;
                }
                case Down:
                {
                    this._txt.x = -130 / 2;
                    this._txt.y = -(vh - (70 - this.getLabelHeight()) / 2);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function getLabelHeight() : int
        {
            return this._txt.numLines * 19;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._lastSetted = -11111;
            this._bmp.dispose(param1);
            this._bmp = null;
            this._txt.dispose(param1);
            this._txt = null;
            this._container.dispose(param1);
            this._container = null;
            this._curDirection = Right;
            this.removeEventListener(Event.ENTER_FRAME, this.everyFrameHandler);
            return;
        }// end function

    }
}
